package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.utils.StringUtils;
import com.liulishuo.qiniuimageloader.QiniuImageLoader;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class MachineListAdapter extends RecyclerView.Adapter<MachineListAdapter.ViewHolder> {
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    private Context mContext;
    private List<String> mUrls;
    private View mHeaderView;
    public static Resources mResource;


    public MachineListAdapter(Context mContext, List<String> urls) {
        this.mContext = mContext;
        this.mUrls = urls;
    }

    public void setResource(Resources mResource) {
        this.mResource = mResource;
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_item_album_bg;                // 组背景图


        public ViewHolder(View itemView) {
            super(itemView);
            if (itemView == mHeaderView) return;
            iv_item_album_bg = (ImageView) itemView
                    .findViewById(R.id.iv_item_machine_bg);
        }

        public void bindData(final String listItems) {
            if (listItems != null) {
                String url = listItems;
                url = StringUtils.repaceImgUrl(new QiniuImageLoader(mContext, url).halfScreenW().formatWebp().createQiniuUrl());
//                final KenBurnsView iv_image_browser = (KenBurnsView) view.findViewById(R.id.iv_image_browse);
                Log.i("xjs", "uuuuuuu==>" + url);
                ImageLoader.getInstance().displayImage(url, iv_item_album_bg);
//                Glide.with(mContext).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).into(iv_item_album_bg);
            }
        }
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    public View getHeaderView() {
        return mHeaderView;
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null) return TYPE_NORMAL;
        if (position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    private String getItem(int position) {
        return mUrls.get(position);
    }

    // 创建ViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER) return new ViewHolder(mHeaderView);
        View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_item_machine, parent, false);
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) return;
        final int pos = getRealPosition(holder);
        String listItems = getItem(pos);
        holder.bindData(listItems);
        holder.itemView.setTag(listItems);
    }


    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    @Override
    public int getItemCount() {
        return mHeaderView == null ? mUrls.size() : mUrls.size() + 1;
    }


}
